package com.winchain.chainbill.bill.biz.repurchase.cmd;

import com.shell.bee.acct.transaction.po.AcctTxRec;
import com.shell.bee.base.utils.DateUtil;
import com.shell.framework.command.CallbackContext;
import com.shell.framework.command.CommandContext;
import com.shell.framework.command.ICallback;
import com.shell.framework.command.ICommand;
import com.shell.framework.utils.CommonDataUtil;
import com.shell.framework.utils.MessageSender;
import com.shell.governor.modules.message.keyword.RpurFinerKeyword;
import com.shell.governor.modules.message.keyword.RpurSuccessKeyword;
import com.winchain.chainbill.basic.cus.service.CusRecService;
import com.winchain.chainbill.bill.base.po.CbDct;
import com.winchain.chainbill.bill.base.po.CbRec;
import com.winchain.chainbill.bill.base.po.CbRpur;
import com.winchain.chainbill.bill.base.service.CbDctService;
import com.winchain.chainbill.bill.biz.Bill;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BillRpurMsgCmd implements ICommand<Bill<CbRpur>>, ICallback<Bill<CbRpur>, AcctTxRec> {
	@Autowired
	CusRecService cusRecService;
	@Autowired
	CbDctService cbDctService;

	public boolean execute(CommandContext<Bill<CbRpur>> context) throws Exception {
		CbRec cbRec = ((Bill) context.getSource()).getRec();
		CbRpur cbRpur = (CbRpur) ((Bill) context.getSource()).getSource();

		String template = "MT02616";
		RpurSuccessKeyword bKeyWord = new RpurSuccessKeyword();
		bKeyWord.setBillAmt(String.valueOf(cbRpur.getRpurAmt()));
		bKeyWord.setRpurDate(DateUtil.parseDate(cbRpur.getCreateTime(), "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd"));

		String senderId = CommonDataUtil.getUserId();
		String buyerId = cbRpur.getBuyerId();
		MessageSender.sendMessageByTmpl(senderId, template, bKeyWord, buyerId);

		CbDct data = (CbDct) this.cbDctService.selectByKey(cbRpur.getDctId());
		String finId = data.getFinId();
		template = "MT01502";
		RpurFinerKeyword fKeyWord = new RpurFinerKeyword();
		fKeyWord.setBillAmt(String.valueOf(cbRpur.getRpurAmt()));
		fKeyWord.setRpurDate(DateUtil.parseDate(cbRpur.getCreateTime(), "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd"));
		fKeyWord.setBillCode(cbRec.getBillCode());
		fKeyWord.setBuyerNm(cbRpur.getBuyerNm());
		MessageSender.sendMessageByTmpl(senderId, template, fKeyWord, finId);

		return false;
	}

	public boolean callback(CallbackContext<Bill<CbRpur>, AcctTxRec> context) throws Exception {
		execute(context);
		return false;
	}
}
